<?php

namespace app\company\controller;

use app\admin\model\GoodsCate;
use app\admin\model\GoodsRule;
use app\company\validate\CompanyOrderValidate;
use app\service\traits\Crud;
use app\company\model\CompanyOrder as CompanyOrderModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

class CompanyOrder extends Base
{
    private CompanyOrderModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new CompanyOrderModel();
        $this->validateClass = CompanyOrderValidate::class;
        $this->append        = 'order_status_text';
        $this->hidden        = 'type,pay_type,voucher,delete_time';
        $this->withSearch    = 'id,no,order_status,create_time';
        $this->isGs          = true;
    }


    public function format($list): mixed
    {
        return $list->each(function ($item) {
            $item->goods_type_count = $item->companyOrderDetails()->count();//商品种类
            $item->goods_count      = $item->companyOrderDetails()->sum('quantity');//商品总数
            $item->goods_amount     = $item->companyOrderDetails()->sum('amount');//商品总价
        });
    }

    public function detailFormat($info): mixed
    {
        $info->details   = $info->companyOrderDetails->each(function ($item) {
            $item->goods          = $item->goods()->withTrashed()->value('name');
            $item->goods_rule     = $item->goodsRule()->withTrashed()->field('spec_name,img')->find();
            $item->storage_name   = ''; //获取库位名称
            $item->storage_detail = []; //获取库位详情
            if ($storage = $item->companyOrderStorages()->column('warehouse_storage_name')) {
                $item->storage_name   = implode(',', $storage);
                $item->storage_detail = $item->companyOrderStorages()->field('id,warehouse_storage_id,warehouse_storage_name,quantity')->select();
            }
            $item->production_time = setDates($item->production_time); //生产时间
            $item->expire_time     = setDates($item->expire_time); //过期时间
        });
        $info->order_log = $info->companyOrderLogs()->select();
        return $info;
    }

    /**
     * 获取商品列表(进货时所用接口)
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function goodsList(): Response
    {
        $param = request()->param();
        $model = new GoodsRule();
        $model = $model->alias('r')->join('goods g', 'r.goods_id = g.id');
        if (request()->param('goods_cate_id', 0)) {
            $model = $model->where('g.goods_cate_id', $param['goods_cate_id']);
        }
        if (request()->param('name', 0)) {
            $model = $model->where('g.name', 'like', '%' . $param['name'] . '%');
        }
        $status = $param['status'] ?? 1;
        $model  = $model->where('g.status', $status);
        $count  = $model->count();
        $model  = $model->page($param['page'], $param['limit']);
        $list   = $model->field("r.money,r.goods_id,r.id as goods_rule_id,r.spec_name,r.purchasing_price,r.img,g.name as goods_name")->select();
        return $this->success('获取成功', compact('count', 'list'));
    }

    /**
     * 商品分类列表
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function cateList(): Response
    {
        $param = Request::param();
        $sql   = new GoodsCate();
        $sql   = $sql->withSearch($this->withSearch, $param);
        $field = 'id';
        if (request()->param('field')) $field .= ',' . request()->param('field');
        $list = $sql->field($field)->order($this->orderSort)->where('pid', 0)->select()->each(function ($item) {
            $item->children = GoodsCate::where('pid', $item->id)->field('id,name')->select();
        });
        return $this->success('获取成功', compact('list'));
    }
}